﻿#include "qmlengine.h"

QmlEngine::QmlEngine(QObject *parent)
    : QQmlApplicationEngine(parent)
{

    QObject::connect(m_dataManager,SIGNAL(getAllParameter_Signal(ParameterModel*)),this,SLOT(getAllModel_Slots(ParameterModel*)));

    initData();

    registerType();
}

void QmlEngine::initData()
{
    m_dataManager->isExistsTable();
}

void QmlEngine::registerType()
{
    // 将重写的QMLEngine 注册到上下文中
    this->rootContext()->setContextProperty("Cpp_Qmlengine",this);
    // 自定义的CppModel 注册到上下文中
    this->rootContext()->setContextProperty("Cpp_ItemModel",m_ItemModel);

    // 连接控制类【ModBusTCP ，打印机 ， 扫码枪】
    this->rootContext()->setContextProperty("Cpp_Connect",m_Connect);
}

void QmlEngine::qmlDeleteDataByIndex(int index)
{
    if(m_ItemModel->count() > index )
    {
        int id = m_ItemModel->itemAt(index)->id();

        m_dataManager->deleteDataByID(id);
    }

    qDebug()<<"Cpp:qmlDeleteDataByIndex:"<<index;
}

void QmlEngine::qmlAddData()
{
    qDebug()<<"Cpp:qml添加数据";
    m_dataManager->addData();
}

void QmlEngine::updateCurrentLocation(QString knifeCrrt, QString rollerCrrt,
                                      QString stress01Crrt, QString stress02Crrt, QString stress03Crrt,
                                      QString f_knife01Crrt, QString f_knife02Crrt, QString f_knife03Crrt,
                                      QString b_knife01Crrt, QString b_knife02Crrt, QString b_knife03Crrt)
{
    qDebug()<<"Cpp:上载数据";
    m_dataManager->uploadCrrtLocatoinData( knifeCrrt,  rollerCrrt,
                                           stress01Crrt,  stress02Crrt,  stress03Crrt,
                                           f_knife01Crrt,  f_knife02Crrt,  f_knife03Crrt,
                                           b_knife01Crrt,  b_knife02Crrt,  b_knife03Crrt);
}

void QmlEngine::qmlSaveData(int index,QString key,QString value)
{
    qDebug()<<"Cpp:qml保存数据";
    if(m_ItemModel->count() > index)
    {
        int id = m_ItemModel->itemAt(index)->id();
        m_dataManager->saveData(id,key,value);
    }
}

void QmlEngine::qmlSearchData(QString find)
{
    m_dataManager->searchData(find);
}

void QmlEngine::qmlRefreshModelData()
{
    m_dataManager->getAllParameterFromSQL();
}

void QmlEngine::getAllModel_Slots(ParameterModel *ItemModel)
{
    m_ItemModel->copyModel(ItemModel);
}
